package com.company.ui;

import com.company.MyLinkList;
import com.company.Student;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by v on 16-12-20.
 */
public class StudentUI extends JPanel{
    private MyLinkList<Student> students;
    private ListPanel listPanel;
    private StudentDialog dialog;
    private boolean changeAble=false;

    private JButton jbtAdd=new JButton("增加一个学生");
    private JButton jbtRemove=new JButton("删除该学生");
    private JButton jbtChange=new JButton("修改该学生的信息");
    private JButton jbtMessage=new JButton("详细信息");

    public StudentUI(){
        students=new MyLinkList<>();
        listPanel=new ListPanel();
        dialog=new StudentDialog();
        JPanel panel=new JPanel();
        panel.add(jbtAdd);
        panel.add(jbtRemove);
        panel.add(jbtChange);
        panel.add(jbtMessage);

        setLayout(new BorderLayout());
        add(panel,BorderLayout.NORTH);
        add(listPanel,BorderLayout.CENTER);

        jbtAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!changeAble){
                    return;
                }

                dialog.insertDialog();
            }
        });

        jbtChange.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(listPanel.target==-1
                        ||listPanel.target>=students.size()){
                    return;
                }

                dialog.changeDialog(students.get(listPanel.target));
            }
        });

        jbtRemove.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(listPanel.target==-1
                        ||listPanel.target>=students.size()){
                    return;
                }
                students.remove(listPanel.target);
                listPanel.rebuild();
            }
        });

        jbtMessage.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(listPanel.target==-1
                        ||listPanel.target>=students.size()){
                    return;
                }

                Student student=students.get(listPanel.target);

                JOptionPane.showMessageDialog(null,
                        "学号:"+student.getStudentNumber()+
                                "\n姓名:"+student.getName()+
                                "\n生日:"+student.getBirthDay()+
                                "\n性别:"+student.getSex()+
                                "\n健康状况:"+student.getHealthy());
            }
        });
    }

    //滚动Panel
    private class ListPanel extends JScrollPane{
        private JList<Student> myList;
        private ListModel<Student> listModel;

        //被选中的目标
        private int target=-1;

        public ListPanel(){
            listModel=new DefaultComboBoxModel(students.toArray());
            myList=new JList<>();
            myList.setFont(new Font("微软雅黑",0,20));
            myList.setModel(listModel);
            this.setViewportView(myList);

            //多选框监听器，对选中单位放入到target
            myList.addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    target=myList.getSelectedIndex();
                }
            });
        }

        public void rebuild(){
            listModel=new DefaultComboBoxModel(students.toArray());
            myList.setModel(listModel);
        }
    }

    public void setStudents(MyLinkList<Student> students) {
        this.students = students;
        changeAble=true;
        listPanel.rebuild();
    }

    public void rebuild(){
        listPanel.rebuild();
    }


    public class StudentDialog extends JDialog{
        private JButton jbtOK=new JButton("OK");
        private JButton jbtCancel=new JButton("Cancel");

        private JLabel jlbName=new JLabel("学生的姓名");
        private JLabel jlbNumber=new JLabel("学生学号");
        private JLabel jlbDate=new JLabel("出生日期");
        private JLabel jlbSex=new JLabel("学生性别");
        private JLabel jlbHealth=new JLabel("学生健康状况");

        private boolean isChange;

        private JTextField[] fields=new JTextField[5];
        private JLabel[] labels=new JLabel[5];

        //Student的对话框
        public StudentDialog(){
            super((Dialog) null,true);

            setFont(new Font("微软雅黑",0,20));

            for(int i=0;i<fields.length;i++){
                fields[i]=new JTextField(20);
            }
            labels[0]=jlbNumber;
            labels[1]=jlbName;
            labels[2]=jlbDate;
            labels[3]=jlbSex;
            labels[4]=jlbHealth;

            setSize(400,200);

            setLocationRelativeTo(null);

            JPanel panel=new JPanel();
            panel.setLayout(new GridLayout(5,2));
            for(int i=0;i<5;i++){
                panel.add(labels[i]);
                panel.add(fields[i]);
            }
            add(panel,BorderLayout.CENTER);

            JPanel panel1=new JPanel();
            panel1.add(jbtOK);
            panel1.add(jbtCancel);
            add(panel1,BorderLayout.SOUTH);

            jbtCancel.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    setVisible(false);
                    isChange=false;
                    clear();
                }
            });

            jbtOK.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Student student=new Student(fields[0].getText(),
                            fields[1].getText(), fields[2].getText(),
                            fields[3].getText(),fields[4].getText());

                    if (isChange){
                        students.set(student,listPanel.target);
                    }else {
                        students.add(student);
                    }

                    //除去对话框中的各种元素
                    setVisible(false);
                    isChange=false;
                    clear();
                    rebuild();
                }
            });
        }

        //插入选项框
        public void insertDialog(){
            setVisible(true);
        }

        //修改选项框
        public void changeDialog(Student student){
            fields[0].setText(student.getStudentNumber());
            fields[1].setText(student.getName());
            fields[2].setText(student.getBirthDay());
            fields[3].setText(student.getSex());
            fields[4].setText(student.getHealthy());
            isChange=true;

            setVisible(true);
        }

        private void clear(){
            for(int i=0;i<fields.length;i++){
                fields[i].setText("");
            }
        }
    }
}
